package com.example.yuejian23.yuejian_23.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian23.yuejian_23.Data.Mdata;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.GuanzhuBean;
import com.example.yuejian23.yuejian_23.javaBean.GuanzhusBean;
import com.example.yuejian23.yuejian_23.javaBean.HuodongBean;
import com.example.yuejian23.yuejian_23.javaBean.JulebuBean;
import com.example.yuejian23.yuejian_23.service.GuanzhuService;
import com.example.yuejian23.yuejian_23.service.HomeService;
import com.example.yuejian23.yuejian_23.service.JulebuService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HuodongActivity extends AppCompatActivity {
    String huodong;
    TextView huodong1_name,tv_goods_fitt_all_price,num;
    ImageView numadd, numjian;
    ImageView huodong1_pic,xin;
    TextView name1;
    TextView jingfei1;
    TextView where1;
    TextView shijian1;
    TextView num1;
    TextView jieshao1;
    int guanzhu;

    LinearLayout shouji;
    Button bao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huodong);

        intview();
        Bundle bundle = this.getIntent().getExtras();
        huodong = bundle.getString("huodongid");
        xin = (ImageView) findViewById(R.id.xin);
        xin.setOnClickListener(new guanzhus());

        numadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float price = Float.valueOf(jingfei1.getText().toString().trim());
                int snum = Integer.valueOf(num.getText().toString().trim());
                snum++;
                String zs = String.valueOf(snum);
                num.setText(zs);
                float zj = price * snum;
                String newzj = String.valueOf(zj);
                //      Toast.makeText(FoodinfoActivity.this,newzj,Toast.LENGTH_LONG).show();

                tv_goods_fitt_all_price.setText(newzj);

            }
        });
        numjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float p = Float.valueOf(jingfei1.getText().toString().trim());
                int s = Integer.valueOf(num.getText().toString().trim());
                float z = Float.valueOf(tv_goods_fitt_all_price.getText().toString().trim());

                //  Toast.makeText(FoodinfoActivity.this,String.valueOf(s--),Toast.LENGTH_LONG).show();
                if (s == 0) {

                } else {
                    s--;
                    String news = String.valueOf(s);
                    num.setText(String.valueOf(news));

                    tv_goods_fitt_all_price.setText(String.valueOf(z - p));
                }
            }
        });

        getjulebudetil();
        guanzhu();
    }

    private void guanzhu() {
        int flag = 3;
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<GuanzhuBean> call = retrofit.create(GuanzhuService.class).guanzhu(Mdata.uid,huodong,flag);
        call.enqueue(new Callback<GuanzhuBean>() {
            @Override
            public void onResponse(Call<GuanzhuBean> call, Response<GuanzhuBean> response) {
                int state = response.body().getCode();
                guanzhu = state;
                if (state==1){
                    xin.setBackgroundResource(R.drawable.ic_xin);
                }else {
                    xin.setBackgroundResource(R.drawable.ic_xin1);
                }
            }
            @Override
            public void onFailure(Call<GuanzhuBean> call, Throwable t) {

            }
        });
    }

    private class guanzhus implements View.OnClickListener {
        int flag = 3;
        @Override
        public void onClick(View v) {
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            Call<GuanzhusBean> call = retrofit.create(GuanzhuService.class).guanzhus(Mdata.uid,huodong,flag);
            call.enqueue(new Callback<GuanzhusBean>() {
                @Override
                public void onResponse(Call<GuanzhusBean> call, Response<GuanzhusBean> response) {
                    int res = response.body().getCode();
                    if (res==1){
                        if (guanzhu==1){
                            Toast.makeText(HuodongActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                            guanzhu = 0;
                            xin.setBackgroundResource(R.drawable.ic_xin1);
                        }else {
                            Toast.makeText(HuodongActivity.this,"取消关注",Toast.LENGTH_LONG).show();
                            guanzhu = 1;
                            xin.setBackgroundResource(R.drawable.ic_xin);
                        }
                    }
                }
                @Override
                public void onFailure(Call<GuanzhusBean> call, Throwable t) {

                }
            });

        }
    }

    public void getjulebudetil() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<HuodongBean> call = retrofit.create(HomeService.class).getxiang(huodong);
        call.enqueue(new Callback<HuodongBean>() {
            @Override
            public void onResponse(Call<HuodongBean> call, Response<HuodongBean> response) {
                HuodongBean myhuo = response.body();
                huodong1_name.setText(myhuo.getAname());
                name1.setText(myhuo.getAname());
                jingfei1.setText(myhuo.getActp());
                where1.setText(myhuo.getActaddress());
                shijian1.setText(myhuo.getActdata());
                num1.setText(myhuo.getActpone());
                jieshao1.setText(myhuo.getActintro());
                Picasso.with(HuodongActivity.this).load("http://115.159.93.244/new18yuejian/Public/upload/"+myhuo.getActpic()).into(huodong1_pic);
            }

            @Override
            public void onFailure(Call<HuodongBean> call, Throwable t) {

            }
        });

    }
    private void intview() {
        huodong1_name = (TextView) findViewById(R.id.huodong1_name);
        huodong1_pic = (ImageView) findViewById(R.id.huodong1_pic);
        name1 = (TextView) findViewById(R.id.name1);
        jingfei1 = (TextView) findViewById(R.id.jingfei1);
        where1 = (TextView) findViewById(R.id.where1);
        shijian1 = (TextView) findViewById(R.id.shijian1);
        num1 = (TextView) findViewById(R.id.num1);
        jieshao1 = (TextView) findViewById(R.id.jieshao1);
        num = (TextView) findViewById(R.id.fnum);
        tv_goods_fitt_all_price = (TextView) findViewById(R.id.tv_goods_fitt_all_price);
        numadd = (ImageView) findViewById(R.id.numadd);
        numjian = (ImageView) findViewById(R.id.numjian);

        bao = (Button) findViewById(R.id.bao);
        bao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HuodongActivity.this,ZhifuActivity.class);
                startActivity(intent);
            }
        });
        shouji = (LinearLayout) findViewById(R.id.shouji);
        shouji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + "13079890955");
                intent.setData(data);
                startActivity(intent);
            }
        });
    }
}
